package controller;

import bean.Account;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import util.FxmlUtil;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;

public class MainController implements Initializable {
    public Label label;

    public void transferAcc() throws IOException {
        Stage stage = (Stage) this.label.getScene().getWindow();

        Scene scene = FxmlUtil.SCENE.get("transferAcc");
        if (scene == null) {
            AnchorPane pane = FXMLLoader.load(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("fxml/transferAcc.fxml")));
            scene = new Scene(pane);
            FxmlUtil.SCENE.put("transferAcc", scene);
        }
        stage.setScene(scene);
        stage.setTitle("ATM - 转账");
    }

    public void selectAcc() throws IOException {
        Stage stage = (Stage) this.label.getScene().getWindow();

        Scene scene = FxmlUtil.SCENE.get("selectAcc");
        if (scene == null) {
            AnchorPane pane = FXMLLoader.load(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("fxml/selectAcc.fxml")));
            scene = new Scene(pane);
            FxmlUtil.SCENE.put("selectAcc", scene);
        }
        stage.setScene(scene);
        stage.setTitle("ATM - 查看");
    }

    public void deposit() {
        FxmlUtil.DATA.put("type", 1);//存钱

        Stage stage = (Stage) this.label.getScene().getWindow();
        stage.setScene(FxmlUtil.SCENE.get("operate"));
        stage.setTitle("ATM - 存钱");
    }

    public void withdrawal() {
        FxmlUtil.DATA.put("type", 2);//取钱

        Stage stage = (Stage) this.label.getScene().getWindow();
        stage.setScene(FxmlUtil.SCENE.get("operate"));
        stage.setTitle("ATM - 取钱");
    }

    public void logout() {
        this.label.getScene().getWindow().hide();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Account account = (Account) FxmlUtil.DATA.get("account");
        label.setText("欢迎您：" + account.getRealName() + "，你的卡号为：" + account.getAccNo());

        try {
            Scene scene = FxmlUtil.SCENE.get("operate");
            if (scene == null) {
                AnchorPane pane = FXMLLoader.load(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("fxml/operate.fxml")));
                scene = new Scene(pane);
                FxmlUtil.SCENE.put("operate", scene);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
